package zjl.graph;

public class Edge implements BaseEdge, Comparable<Edge> {

    private static int currentEdgeId = 0;  // Uniquely identify each edge

    // 链路标识
    private int id;

    private BaseVertex startVertex;
    private BaseVertex endVertex;

    // 边权重值
    private double weight;

    public Edge(BaseVertex b, BaseVertex e, double weight) {
        this.id = currentEdgeId;
        this.startVertex = b;
        this.endVertex = e;
        this.weight = weight;
        currentEdgeId++;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public BaseVertex getStartVertex() {
        return startVertex;
    }

    @Override
    public BaseVertex getEndVertex() {
        return endVertex;
    }

    @Override
    public double getWeight() {
        return weight;
    }

    @Override
    public void setWeight(double weight) {
        this.weight = weight;
    }

    public static void reset() {
        currentEdgeId = 0;
    }

    @Override
    public int compareTo(Edge that) {
        double diff = this.weight - that.weight;
        if (diff > 0) {
            return 1;
        } else if (diff < 0) {
            return -1;
        }

        return 0;
    }

    @Override
    public String toString() {
        return "E<" + id + ">(" + + startVertex.getId() + "-" + endVertex.getId() + ")";
    }

}
